#!/bin/sh
#
# Shell for the user that will run MySQL in a chroot jail. This user does not
# have an encrupted file system.
#
# To start the database, you must be root, type "su $CHROOT_NAME".
# To stop the database, as root, type...
# chroot /home/chroot/$CHROOT_NAME /etc/init.d/mysql stop
#
#
# enchroot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# enchroot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with enchroot. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (c) 2010 charles@kollar.com
#

#
# Global configuraiton/options are set in this file...
. /usr/local/etc/enchroot.conf

JAILPATH=$HOME_CHROOT/$USER

#
# No $HOME is needed here since this shell only lives to make sure that
# the appropriate directories are mounted for this instance of MySQL,
# and then run MySQL for the appointed user.

#
# If any of these file systems have been mounted, don't mount them again...
#
# Also, remember to add the dismounting of any new directories mentioned here to
# the script 'delete_chroot'.

DEV_P=`/bin/grep "$JAILPATH/dev" /etc/mtab`
if [ "$DEV_P" == "" ] ; then
  /usr/bin/sudo /bin/mount -o bind /dev $JAILPATH/dev
fi

PROC_P=`/bin/grep "$JAILPATH/proc" /etc/mtab`
if [ "$PROC_P" == "" ] ; then
  /usr/bin/sudo /bin/mount -o bind /proc $JAILPATH/proc
fi

#
# Here we are mounting the directory that contains the database in our chroot jail.
# All of the jails have the database mounted in the same place.
DATA_P=`/bin/grep "$JAILPATH/var/lib/mysql" /etc/mtab`
if [ "$DATA_P" == "" ] ; then
  /usr/bin/sudo /bin/mount -o bind /home/mysql/$USER/data $JAILPATH/var/lib/mysql
fi

#
# It is important to 'cd' to a point within the user's mount point 'before' issuing
# the 'chroot' command. Otherwise the current directory of the user is not within
# their portion of the file system.
cd $JAILPATH/home/$USER

# Note:
# The '/bin/su' that is called here is the one in the chroot jail.
/usr/bin/sudo /usr/bin/chroot $JAILPATH /bin/su --command='/etc/init.d/mysql start' $USER
